package com.douma.linkedlist;


import java.util.Stack;

public class LinkedListRecursion {

    // 迭代
    // 时间复杂度：O(n)
    // 空间复杂度：O(1)
    public static void printListIter(ListNode head) {
        while (head != null) {
            System.out.println(head.val);
            head = head.next;
        }
    }

    // 递归
    public static void printList(ListNode head) {
        if (head == null) return;

        System.out.println(head.val);

        printList(head.next);
    }

    public static void printList_1(ListNode head) {
        if (head == null) return;

        Stack<ListNode> stack = new Stack<>();
        stack.push(head);

        while (!stack.isEmpty()) {
            ListNode node = stack.pop();
            if (node == null) return;
            System.out.println(node.val);
            stack.push(node.next);
        }
    }


}
